import type { IPlayerInfo, IPlayerInfoUI } from '../data/playerInfo';

export class PlayerInfo {
    /**
     * 用户呢称 / User nickname
     */
    private _user: string = '';
    /**
     * 称号 / Title
     */
    private _title: string = '';
    /**
     * 宠币 / Coin
     */
    private _coin: number = 0;
    /**
     * 等级 / Level
     */
    private _level: number = 1;
    /**
     * 经验 / Experience
     */
    private _exp: number = 0;
    /**
     * 宠物只数 / Number of pets
     */
    private _petCount: number = 0;
    /**
     * 头像 / Avatar
     */
    private _avatar: string = '';
    /**
     * 用户昵称UI / User nickname UI
     */
    private _userUI: UiText | null;
    /**
     * 称号UI / Title UI
     */
    private _titleUI: UiText | null;
    /**
     * 宠币UI / Coin UI
     */
    private _coinUI: UiText | null;
    /**
     * 等级UI / Level UI
     */
    private _levelUI: UiText | null;
    /**
     * 宠物只数UI / Number of pets UI
     */
    private _petCountUI: UiText | null;
    /**
     * 头像UI / Avatar UI
     */
    private _avatarUI: UiImage | null;
    /**
     * 构造函数 / Constructor
     */
    public constructor(config: IPlayerInfoUI) {
        console.log('(Client) PlayerInfo constructor');
        this._userUI = config.userUI || null;
        this._titleUI = config.titleUI || null;
        this._coinUI = config.coinUI || null;
        this._levelUI = config.levelUI || null;
        this._petCountUI = config.petCountUI || null;
        this._avatarUI = config.avatarUI || null;
    }

    public update(data: IPlayerInfo): void {
        this._user = data.user;
        this._title = data.title;
        this._coin = data.coin;
        this._level = data.level;
        this._exp = data.exp;
        this._petCount = data.petCount;
        this._avatar = data.avatar;
        if (this._userUI) {
            this._userUI.textContent = this._user;
        }
        if (this._titleUI) {
            this._titleUI.textContent = this._title;
        }
        if (this._coinUI) {
            this._coinUI.textContent = this._coin.toString();
        }
        if (this._levelUI) {
            this._levelUI.textContent = this._level.toString();
        }
        if (this._petCountUI) {
            this._petCountUI.textContent = this._petCount.toString();
        }
        if (this._avatarUI) {
            this._avatarUI.image = this._avatar;
        }
    }
}
